<template>
    <div>
        <mHeader></mHeader>
        <div class="main-con">
            <div class="left_main">
                <leftMenu />
            </div>
            <div class="rightMain">
                <div class="r_main">
                    <div style="flex:1">
                        <div class="tabIn">
                            <div class="tab">

                                <span class="title" style="flex-shrink: 0;white-space: nowrap;">榜单</span>

                                <div v-for="item in tabList" :key="item.id" @click="getTabtype(item.id)">
                                    <span :class="{ actives: item.id == curType }">{{ item.name }}</span>
                                    <div class="botLine">
                                        <span :class="{ active: item.id == curType }"></span>
                                    </div>
                                </div>
                            </div>
                            <div class="date">
                                <span>【</span>
                                <div>
                                    <span @click="getDate(1)" :class="{ active: dateType == 1 }">今日</span>
                                    <span class="line"></span>
                                    <span @click="getDate(2)" :class="{ active: dateType == 2 }">本周</span>
                                    <span class="line"></span>
                                    <span @click="getDate(3)" :class="{ active: dateType == 3 }">月度</span>
                                </div>
                                <span>】</span>
                            </div>
                        </div>
                        <div class="form_con">
                            <div class="list_box" v-for="(item, index) in menuList" :key="index"
                                @click="goPage('/columnIndex', item)">
                                <img :src="item.avatar" alt="">
                                <div class="list_t">
                                    <span class="line2">{{ item.title }}</span>
                                    <div>
                                        <span class="line1" @click="goPage('/association/youping', item.user_id)">{{
                                            item.user_nickname }}</span>
                                        <div class="eye">
                                            <img src="@/assets/img/eye.png" alt="">
                                            <span>{{ item.views }}</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <el-empty description="没有内容了" v-if="menuList.length == 0"></el-empty>

                    </div>
                    <div class="tabIn2">
                        <div class="tab">
                            <div v-for="item in tabList2" :key="item.type" @click="getTabtype2(item.type)">
                                <span :class="{ actives: item.type == curType2 }" style="white-space: nowrap;">{{
                                    item.name }}</span>
                                <div class="botLine">
                                    <span :class="{ active: item.type == curType2 }"></span>
                                </div>
                            </div>
                        </div>
                        <div class="titleList" v-infinite-scroll="load" :infinite-scroll-distance="10"
                            :infinite-scroll-immediate="false">
                            <div v-for="(item, index) in list" :key="item.id" @click="goPageNow(item.id)">
                                <p :class="{ line2: true, active: item.id == id }">
                                    <span :class="{ sort: true, sort1: index <= 2 }">{{ index + 1 }}</span>
                                    {{ item.title }}
                                </p>
                            </div>
                            <el-empty description="没有内容了" v-if="list.length == 0"></el-empty>

                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</template>
<script>
import leftMenu from '@/components/leftMenu.vue';
export default {
    components: {
        leftMenu
    },
    data() {
        return {
            menuList: [],
            curType: 0,
            tabList: [],
            tabList2: [{ name: '热点', type: 1 }, { name: '优品', type: 2 }, { name: '同城', type: 3 }, { name: '发文', type: 4 }],
            curType2: 1,
            type: 1,
            list: [],
            id: '',
            dateType: 1,
            totalPage: 1,
            pageNum: 1,
            loading: false
        };
    },
    computed: {
        path() {
            return this.$route.path
        }
    },
    beforeDestroy() {
        window.removeEventListener('scroll', this.handleScroll); // 移除监听
    },

    mounted() {
        this.getType()
        this.getList()
        this.getRight()
        window.addEventListener('scroll', this.handleScroll); // 监听滚动事件

    },

    methods: {

        handleScroll() {
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            const windowHeight = window.innerHeight;
            const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
            // 判断是否滚动到底部
            if (scrollTop + windowHeight >= scrollHeight - 20) {
                console.log('触底');

                this.load()
            }
        },

        goPage(url, item) {
            let type = item.type
            let id = item.id
            if (type == 'col') {
                // 另页打开
                const route = this.$router.resolve({
                    path: url,
                    query: { id }
                });
                window.open(route.href, '_blank');

            } else {
                // 另页打开
                const route = this.$router.resolve({
                    path: '/articleRead',
                    query: { aid: id }
                });
                window.open(route.href, '_blank');


            }
        },

        goPageNow(id) {
            let type = this.curType2

            // 文库
            if (type == 1 || type == 3) {
                this.$router.push({
                    path: '/articleRead',
                    query: {
                        aid: id
                    }
                })
            }

            // 专栏
            if (type == 2) {
                this.$router.push({
                    path: '/columnIndex',
                    query: {
                        id
                    }
                })
            }

            // 账号
            if (type == 4) {
                this.$router.push({
                    path: '/association/youping',
                    query: {
                        id
                    }
                })
            }


        },
        getRight() {
            let params = {
                type: this.curType2
            }

            this.$api.getRight(params).then(res => {
                console.log(res)
                if (res.data.code == 1) {
                    this.list = res.data.data;
                }

            })
        },
        getList() {
            this.loading = true
            let params = {
                page: this.pageNum,
                tid: this.curType,
                type: this.dateType
            }

            this.$api.getRank(params).then(res => {
                console.log(res)
                if (res.data.code == 1) {
                    this.totalPage = res.data.data.last_page
                    this.menuList = this.menuList.concat(res.data.data.data);
                    this.loading = false
                }

            })
        },
        getType() {
            this.$api.rankCate().then(res => {
                if (res.data.code == 1) {
                    this.tabList = [{ id: 0, name: '全部' }, ...res.data.data]

                }
            })
        },
        getDate(t) {
            this.dateType = t
            this.pageNum = 1
            this.menuList = []

            this.getList()
        },
        onsubmit() {
            console.log('submit');

        },
        onsubmit2() {
            console.log('submit');

        },
        load() {
            var page = this.pageNum
            //如果请求未结束就返回不连续请求
            if (this.loading) {
                return
            }
            //数据请求完成无更多数据时
            if (page >= this.totalPage) {
                //this.getzList()

                return false;
            }
            page = parseInt(page) + 1
            this.pageNum = page
            this.getList()
            console.log('触底')
        },
        getTabtype(t) {
            this.curType = t
            this.pageNum = 1
            this.menuList = []

            this.getList()
            // this.$router.push(url)

        },
        getTabtype2(t) {
            this.curType2 = t
            this.list = []
            this.getRight()
        },


    },
}
</script>
<style lang="scss" scoped>
.main-con {
    display: flex;

    .left_main {
        margin-right: 1.04rem;
    }
}

.rightMain {
    flex: 1;

    .r_main {
        width: 100%;
        display: flex;

        .tabIn {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 20px 0 6px;
            background-color: #fff;
            position: sticky;
            top: 98px;
            z-index: 99;
            margin-bottom: 10px;

            .tab {
                display: flex;
                align-items: center;


                .title {
                    font-weight: bold;
                    font-size: 1.46rem;
                    color: #000000;
                    white-space: nowrap;

                    margin-right: 30px;
                }

                >div {
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    margin-right: 40px;
                    cursor: pointer;
                    position: relative;

                    .actives {
                        font-size: 22px;
                        font-weight: bold;
                        color: #1068E7;
                    }

                    .botLine {
                        width: 100%;
                        height: 3px;
                        box-sizing: content-box;
                        position: relative;
                        top: calc(100% + 5px);
                        position: absolute;
                        left: 0;
                        right: 0;

                        >span {
                            position: absolute;
                            left: 0;
                            top: 0;
                            display: inline-block;
                            width: 0;
                            height: 3px;
                            background-color: #1068E7;
                            border-radius: 3px;
                        }

                        .active {
                            animation: expandWidth .5s forwards;
                        }

                        @keyframes expandWidth {
                            0% {
                                width: 0;
                            }

                            100% {
                                width: 100%;
                            }
                        }
                    }
                }
            }

            .date {
                display: flex;
                align-items: center;

                >div {
                    display: flex;
                    font-size: 18px;
                    align-items: center;

                    >span {
                        display: block;
                        cursor: pointer;
                    }

                    .line {
                        margin: 0 10px;
                        display: block;
                        width: 1px;
                        height: 18px;
                        background-color: #000000;
                    }

                    .active {
                        color: #1068E7;
                        font-weight: bolder;
                        font-size: 24px;
                    }
                }
            }

        }

        .tabIn2 {
            margin: 0px 0 0 20px;
            padding: 20px;
            background-color: #E7F1FE;
            z-index: 99;
            width: 300px;
            position: sticky;
            top: 118px;
            height: calc(100vh - 250px);
            min-height: 200px;

            .tab {
                display: flex;
                align-items: center;
                padding-bottom: 20px;
                background-color: #E7F1FE;

                >div {
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    margin-right: 30px;
                    cursor: pointer;
                    position: relative;

                    &:last-child {
                        margin-right: 0;
                    }

                    .actives {
                        font-weight: bold;
                        font-size: 23px;
                        color: #1068E7;
                    }

                    .botLine {
                        width: 100%;
                        height: 3px;
                        box-sizing: content-box;
                        position: relative;
                        top: calc(100% + 5px);
                        position: absolute;
                        left: 0;
                        right: 0;

                        >span {
                            position: absolute;
                            left: 0;
                            top: 0;
                            display: inline-block;
                            width: 0;
                            height: 3px;
                            background-color: #1068E7;
                            border-radius: 3px;
                        }

                        .active {
                            animation: expandWidth .5s forwards;
                        }


                        @keyframes expandWidth {
                            0% {
                                width: 0;
                            }

                            100% {
                                width: 100%;
                            }
                        }
                    }
                }
            }

            .titleList {
                height: calc(100vh - 350px);
                overflow-y: auto;
                -ms-overflow-style: none;


                >div {
                    cursor: pointer;
                    padding: 20px 0;
                    border-bottom: 1px solid #CDCCCA;

                    .sort {
                        display: inline-block;
                        width: 20px;
                        height: 20px;
                        text-align: center;
                        line-height: 20px;
                        color: #fff;
                        background-color: #999;
                        font-size: 15px;
                        margin-top: 2px;
                        flex-shrink: 0;
                        margin-right: 10px;
                    }

                    .sort1 {
                        display: inline-block;
                        width: 20px;
                        height: 20px;
                        text-align: center;
                        line-height: 20px;
                        color: #fff;
                        background-color: red;
                        font-size: 15px;
                        margin-top: 2px;
                        flex-shrink: 0;
                        margin-right: 10px;
                    }

                    p {
                        display: flex;

                    }

                    .active {
                        .sort {
                            background-color: #1068E7;
                        }

                        color: #1068E7;
                    }

                    &:hover {
                        .sort {
                            background-color: #1068E7;
                        }

                        color: #1068E7;
                    }
                }
            }

        }

        .titleList::-webkit-scrollbar {
            display: none;
        }

        .form_con {
            //            min-height: 500px;
            display: flex;
            flex-wrap: wrap;
            // overflow-y: auto;

            .list_box {
                cursor: pointer;
                display: inline-flex;
                align-items: center;
                width: calc(50% - 10px);
                height: 130px;
                border: 1px solid #C3DCFB;
                padding: 1.04rem 1.6rem 0.94rem 0.99rem;
                margin: 10px 0;

                &:nth-child(2n) {
                    margin-left: 20px;
                }

                >img {
                    width: 80px;
                    height: 80px;
                    border-radius: 50%;
                    flex-shrink: 0;
                }

                .list_t {
                    flex: 1;

                    margin-left: 0.99rem;
                    display: flex;
                    justify-content: space-between;
                    flex-direction: column;
                    height: 100%;

                    >span {
                        font-weight: 400;
                        font-size: 22px;
                        color: #000000;
                    }

                    >div {
                        display: flex;

                        align-items: center;
                        justify-content: space-between;

                        >span {
                            font-weight: 400;
                            font-size: 18px;
                            color: #999999;
                        }

                        .resou {
                            font-weight: 400;
                            font-size: 18px;
                            color: #999999;

                            >span {
                                color: #1068E7;
                            }
                        }

                        .eye {
                            font-weight: 400;
                            font-size: 18px;
                            color: #999999;
                            flex-shrink: 0;
                            display: flex;
                            align-items: center;

                            img {
                                width: 23px;
                                height: 16px;
                                margin-right: 0.3rem;
                            }
                        }
                    }
                }

                .list_t2 {
                    margin-left: 0.99rem;
                    display: flex;
                    justify-content: space-between;
                    flex-direction: column;
                    height: 100%;
                    width: 14.27rem;

                    .name {
                        display: flex;
                        align-items: center;
                        font-weight: 400;
                        font-size: 1.15rem;
                        color: #333333;

                        img {
                            width: 1.25rem;
                            height: 1.25rem;
                            margin-left: 0.7rem;
                            flex-shrink: 0;
                        }
                    }

                    .vip {
                        color: #1068E7;
                    }

                    .sign {
                        width: 100%;
                        font-weight: 400;
                        font-size: 1.04rem;
                        color: #666666;
                    }

                    .hot {
                        display: flex;
                        align-items: center;
                        margin-top: 0.5rem;

                        div {
                            display: flex;
                            align-items: center;
                            font-weight: 400;
                            font-size: 0.94rem;
                            color: #999999;
                            margin-right: 1.6rem;

                            img {
                                width: 1.2rem;
                                height: 1.09rem;
                            }

                            &:nth-child(2) {
                                img {
                                    width: 0.99rem;
                                    height: 0.99rem;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

@media screen and (max-width:768px) {
    .main-con {
        width: 100vw;
    }

    .left_main {
        display: none;
    }

    .rightMain .r_main {
        .tabIn2 {
            display: none;

        }

        .tabIn {
            justify-content: center;
            align-items: flex-start;
            flex-direction: column-reverse;
            padding: 0 15px;

            .date {
                padding: 5px 0;
                border-bottom: 1px solid #ccc;
                margin-bottom: 10px;
                font-size: 13px;

                >span {
                    display: none;
                }

                >div {
                    font-size: 13px;

                    .line {
                        display: none;

                    }

                    span {
                        padding: 0 5px;
                    }

                    .active {
                        font-size: 14px;
                        background-color: #1068E7;
                        color: #fff;
                    }
                }
            }

            .tab {
                width: 100%;
                padding: 40px 0 10px;
                flex-wrap: wrap;
                position: relative;
                display: flex;
                align-items: center;
                justify-content: space-between;

                .title {
                    position: absolute;
                    top: 0px;
                    font-size: 16px;
                    width: 100%;
                    text-align: center;
                    margin-bottom: 10px;
                }

                >div {
                    span {
                        font-size: 14px;
                    }

                    .actives {
                        font-size: 14px;
                    }
                }
            }
        }

        .form_con {
padding: 0 10px;
            .list_box {
                padding: 10px;
                width: 100%;
                height: 100px;
                margin-bottom: 10px;

                >img {
                    width: 70px;
                    height: 70px;
                }

                .list_t {
                    margin-left: 15px;

                    >span {
                        font-size: 16px;
                    }

                    >div {
                        >span {
                            font-size: 16px;
                        }

                    .eye {
                            font-size: 13px;

                            img {
                                width: 15px;
                                height: 9px;
                                margin-right: 5px;
                            }
                        }
                    }
                }

            }
        }
    }
}
</style>